<template>
  <div>
    <div>
      <el-row id="artList" type="flex" justify="space-around">
        <el-col>
          <!--当前板块信息-->
          <el-row class="cdetail">
            <el-card
              style="background: url('https://lyking90.oss-cn-shenzhen.aliyuncs.com/banner.png')  ;background-size: cover;background-position: center  ;"
            >
              <div slot="header">
                <span style="font-style: bold;">{{ cdetail.sectionName }}</span>

                <span style="float: right; padding: 3px 0;color:#2c3e50;"
                  >版主：

                  {{ cdetail.user.username }}</span
                >
              </div>
              <div class="notice" style="color:#34495e;">
                {{ cdetail.notice }}
              </div>
            </el-card>
          </el-row>

          <!--当前板块的帖子-->
          <el-row class="art-item">
            <ul class="list">
              <li v-for="item in postsList" v-bind:key="item" class="list-item">
                <router-link
                  :to="{ name: '详情', params: { postsId: item.id } }"
                >
                  <el-card shadow="hover">
                    <h3>
                      <span class="art-title">{{ item.title }}</span>
                      <el-tag size="mini">{{ item.description }}</el-tag>
                    </h3>
                    <el-row class="art-body">
                      <div class="side-abstract">
                        <div class="art-abstract">
                          {{ item.content }}
                        </div>
                      </div>
                    </el-row>
                    <el-row class="art-info">
                      <div class="message" id="box">
                        <ul class="msg-ul">
                          <li class="msg">
                            <router-link to="/userInfo">
                              <div>
                                <img
                                  class="picture"
                                  :src="item.user.avatar"
                                  style="border-radius:50%;border:1px solid #eee;"
                                />
                                <i style="line-height:30px;color:#aaa;">{{
                                  item.user.username
                                }}</i>
                              </div>
                            </router-link>
                          </li>
                          <li class="msg">
                            <span class="el-icon-time">{{
                              item.createTime
                            }}</span>
                          </li>
                          <li class="msg">
                            <i class="el-icon-view"> {{ item.views }}</i>
                          </li>
                          <li class="msg">
                            <i class="el-icon-s-opportunity">
                              {{ item.likes }}</i
                            >
                          </li>
                        </ul>
                      </div>
                    </el-row>
                  </el-card>
                </router-link>
              </li>
            </ul>
          </el-row>
        </el-col>
      </el-row>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page="curr"
        :page-size="pagesize"
        @current-change="clickPlate"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    categoryId: {
      type: Number,
      default() {
        return 0;
      },
    },
    title: {
      type: String,
      default() {
        return 0;
      },
    },
  },
  data() {
    return {
      id: this.categoryId,
      cdetail: {},
      postsList: [],
      curr: 1,
      total: 0,
      pageSize: 10,
      poststitle: this.title,
    };
  },
  computed: {},
  created: function() {
    this.page(this.curr);
  },
  watch: {
    //监听value的变化，进行相应的操作即可
    categoryId: function(a, b) {
      //a是value的新值，b是旧值
      this.id = a;

      this.clickPlate(this.curr);
      // alert(this.id);
      this.categoryDetail();
    },
    title: function(a, b) {
      this.poststitle = a;
      this.searchByTitle(1);
    },
  },
  methods: {
    //根据板块id获取对应的板块详情信息
    categoryDetail() {
      console.log("查询当前板块详细信息");
      const _this = this;
      this.$axios.post("/forum/category/view?id=" + this.id).then((res) => {
        console.log(res);
        this.cdetail = res.data.data;
      });
    },

    //根据板块id获取对应的帖子
    clickPlate(curr) {
      this.curr = curr;
      console.log("clickPlate当前页：" + this.curr);
      const _this = this;
      this.$axios
        .post(
          "/forum/posts/searchByCategoryId?curr=" +
            this.curr +
            "&categoryId=" +
            this.id
        )
        .then((res) => {
          _this.postsList = res.data.data.list;
          _this.total = res.data.data.total;
          _this.curr = res.data.data.current;
        });
    },
    //推荐板块初始获取， 帖子初始获取
    page(curr) {
      this.curr = curr;
      console.log("初始化page，现在页面：" + this.curr);
      var _this = this;
      this.$axios.post("/forum/category/view?id=" + 1).then((res) => {
        console.log(res);
        this.cdetail = res.data.data;
      });
      this.$axios
        .post(
          "/forum/posts/searchByCategoryId?curr=" +
            this.curr +
            "&categoryId=" +
            1
        )
        .then((res) => {
          console.log(res);
          _this.postsList = res.data.data.list;
          _this.total = res.data.data.total;
        });
    },

    //根据title搜索帖子
    searchByTitle(curr) {
      // 调用接口
      const _this = this;
      this.$axios
        .post(
          "/forum/posts/searchByTitle?curr=" +
            curr +
            "&title=" +
            this.poststitle
        )
        .then((res) => {
          if (res.data.code === 200) {
            console.log(this.currentPage);
            console.log(res);
            _this.postsList = res.data.data.list;
            _this.total = res.data.data.total;
            // _this.curr = res.data.data.current;
            this.$message({
              message: "搜索成功",
              type: "success",
            });
          } else {
            this.$message({
              message: res.data.msg,
              type: "error",
            });
          }
        });
    },
  },
};
</script>

<style scoped>
#box * {
  vertical-align: middle;
}
#side .item {
  margin-bottom: 30px;
}
.el-tag {
  margin-left: 5px;
  vertical-align: middle;
}
.picture {
  width: 30px;
  height: 30px;
  margin-left: 0px;
  margin-right: 5px;
}
.message {
  text-align: left;
  line-height: 30px;
  color: #aaa;
}
.msg-ul {
  margin: 0px;
}

.msg {
  float: left;
  list-style: none;
  line-height: 30px;
  margin-right: 30px;
}
.art-info {
  text-align: left;
}
.art-item {
  position: relative;
}
h3 {
  margin: 0px;
}
.art-item .star {
  width: 60px;
  height: 60px;
  position: absolute;
  top: 0;
  right: 0;
}
.infinite-list-wrapper {
  overflow-x: hidden;
  overflow-y: scroll;
}
.art-title {
  cursor: pointer;
}
.art-title:hover {
  padding-left: 10px;
  color: #ff4040;
}
.art-body {
  display: flex;
  padding: 10px 0;
}
img.art-banner {
  width: 100%;
  height: 100%;
  transition: all 0.6s;
}
img.art-banner:hover {
  transform: scale(1.4);
}
.side-abstract {
  flex: 1;
  display: flex;
  flex-direction: column;
}
.art-abstract {
  flex:1;
  color: #aaa;
  width: 700px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
h5 {
  font-size: 18px;
}
.pagination {
  background-color: #f9f9f9;
}
.infinite-list-wrapper {
  max-height: 900px;
}
.list-item {
  list-style: none;
  text-align: left;
  margin: 5px 0 8px 0;
}

.list-item .el-card:hover {
  box-shadow: 1px 2px 8px #74b9ff;
}
.list-item .el-card:hover div.art-abstract {
  color: #34495e;
}
li.msg:hover {
  color: black;
}

.cdetail {
  margin-top: 10px;
}
</style>
